home *** CD-ROM | disk | FTP | other *** search
- #include <iostream.h> // required for input/output
- #include <Arrays_real.h> // required for array functionality
- #include <CurvPlot.h> // required for curve plotting
-
- int main(int nargs, const char** args)
- {
- initDIFFPACK(nargs, args); // not strictly necessary here, but a good habit
- cout<<"Give number of solution points: ";
- int n; // declare an integer n reflecting the number of divisions
- cin >> n; // read n from standard input (cin)
- real h=1.0/(n+1); // 1/(n+1) gives integer division (=0) so .0 is important!
- Mat(real) A(n,n); // Declaration of a nxn matrix
- Vec(real) b(n); // Declaration of a vector of length n.
- Vec(real) u(n); // Declaration of a vector to hold the numerical solution.
-
- // --- Set up matrix A and vector b ---
- A.fill(0.0); // set all entries in A equal to 0.0
- b.fill(0.0); // set all entries in b equal to 0.0
- for(int i=1;i<=n;i++) // i++ means i=i+1
- {
- if(i != 1) A(i,i-1)= 1; // != means: "not equal"
- if(i != n) A(i,i+1)= 1;
- A(i,i) =-2;
- b(i) =-1*h*h;
- if (i==n) b(i) = b(i)-1;
- }
- A.printAscii(cout,"A before LU-fact."); // print matrix to the screen
- b.printAscii(cout,"right hand side"); // print vector to the screen
- A.factLU(); // Make LU-factorisation
- A.printAscii(cout,"A after LU-fact."); // print matrix w/header
- A.forwBack(b,u); // Solve Au=b
- // in version 1.0: A.forwBackLU(b,u); // Solve Au=b
- cout<<"\n \n x numerical exact:\n"; // \n is newline
- for(i = 1; i <= n; i++)
- cout<<oform("%4.3f %8.5f %8.5f \n",i*h,u(i),i*h*(3-i*h)/2);
-
- // this one requires the gnuplot program to be installed:
- popUpPlot( h, 1-h, u, "gnuplot","700x500+0+0","solution","u");
- // xmin,xmax,solution, program , plot window , title ,func.name
- return 0;
- }
-